package edu.berkeley.cs160.innovationers.shoppersidekick.database;

import edu.berkeley.cs160.innovationers.shoppersidekick.Constants;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;

public class MyDBhelper extends SQLiteOpenHelper {
	private static final String CREATE_TABLE = "create table "+
	Constants.TABLE_NAME+" ("+
	Constants.KEY_ID+" integer primary key autoincrement, "+
	Constants.ITEM_NAME+" text not null, "+
	Constants.QUANTITY_NAME+" text not null, "+
	Constants.LIST_NAME+" text not null);";
	
	public MyDBhelper(Context context, String name, CursorFactory factory, int version){

		super(context, name, factory, version);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db){
		Log.v("MyDBhelper onCreate","Creating all the tables");
		try {
			db.execSQL(CREATE_TABLE);
		}catch(SQLiteException ex){
			Log.v("Create table exception", ex.getMessage());
		}
	}
	
	@Override
	public void onOpen(SQLiteDatabase db){
		Log.v("MyDBhelper onCreate","Creating all the tables");
		try {
			db.execSQL(CREATE_TABLE);
		}catch(SQLiteException ex){
			Log.v("Create table exception", ex.getMessage());
		}
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
		Log.w("TaskDBAdapter", "Upgrading from version "+oldVersion + " to " +newVersion + ", which will estroy all old data");
		db.execSQL("drop table if exists "+Constants.TABLE_NAME);
		onCreate(db);
	}

}